//
//  AMTools.swift
//  微博
//
//  Created by zhc on 15/11/26.
//  Copyright © 2015年 zhc. All rights reserved.
//

import UIKit
import AFNetworking

class AMTools: AFHTTPSessionManager{
    //网络单利
    
    static let sharedInstance: AMTools = {
    
       let baseURL = NSURL(string: "https://api.weibo.com/")
        
        let tool = AMTools(baseURL: baseURL)
        
       //添加响应的序列化器
       tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tool
      
    }()
    
   private let client_id = "4097602553"
    
    private let client_secret = "4f973e5b24a2ae2087103844b2205076"
    //huidiao app
    
    let redirect_uri = "https://www.baidu.com"
    
    func oauthURL() -> NSURL{
    
      let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(client_id)&redirect_uri=\(redirect_uri)"

        return NSURL(string: urlString)!
    
    }
    
    
    
    //MARK:- 网络加载access token
    
    //加载accessToken
    func loadAccessToken(code:String){
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        //请求参数
        let parameters = [
        
            "client_id": client_id,
            "client_secret": client_secret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": redirect_uri
        
        ]
        //发送POST 请求
        
        POST(urlString, parameters: parameters, success: { (_, result) -> Void in
            print("result:\(result)")
            }) { (_, error) -> Void in
                
               // finished(result:nil,error:error)
                
                print("error:\(error)")
                
        }
        
        
        
        
    }
    
    
    
    
    
    
}
